package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.MainPageModuleBookData;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName MainPageModuleBookGvAdapter
 * @Description
 * @date 2016-8-9
 * @history 2016-8-9 author: description:
 */
public class MainPageModuleBookGvAdapter extends AdapterBase<MainPageModuleBookData> {
    private Context context;

    public MainPageModuleBookGvAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (getList() != null && !getList().isEmpty()) {
            if (getList().size() > 3) {// 设置最多显示3个item
                return 3;
            } else {
                return getList().size();
            }
        } else {
            return 0;
        }
    }

    @Override
    protected View getExView(int i, View view, ViewGroup viewGroup) throws Exception {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.draw_book_layout_picbook_gride_item, viewGroup, false);
        }
        RoundImageView bookImg = ViewHolder.get(view, R.id.draw_book_layout_picbook_item_img);
        TextView bookTitle = ViewHolder.get(view, R.id.draw_book_layout_picbook_item_title_tv);
        TextView bookType = ViewHolder.get(view, R.id.draw_book_layout_picbook_item_type_tv);

        MainPageModuleBookData mainPageModuleBookData = getList().get(i);
        String subtitle = mainPageModuleBookData.getSubtitle();
        String description = mainPageModuleBookData.getDescription();
        String coverurl = mainPageModuleBookData.getCoverurl();

        if (coverurl != null) {
            Glide.with(context).load(coverurl).dontAnimate().placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder).diskCacheStrategy(DiskCacheStrategy.ALL).into(bookImg);
        }
        if (subtitle != null) {
            bookTitle.setText(subtitle);
        }
        if (description != null) {
            bookType.setText(description);
        }
        return view;
    }

}
